package com.example.yls.demoa;

import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.BottomSheetDialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private LinearLayout bottomSheet;
    private BottomSheetBehavior bottomSheetBehavior;
    private Button btn_sheet;
    private Button btn_Dialog;
    private boolean isOpen = false;
    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private TextView tv4;

    private BottomSheetDialogFragment bottomSheetDialogFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        intiview();
        bottomSheetEvent();
        btnSheetEvent();
        itemtextViewEvent();
        btnDialogEvent();
    }

    private void btnDialogEvent() {
        btn_Dialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(MainActivity.this);
                bottomSheetDialog.setContentView(R.layout.poem);
                bottomSheetDialog.show();
            }
        });


    }

    //TextView OnClick
    private void itemtextViewEvent() {
        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "tv1响应", Toast.LENGTH_SHORT).show();

            }
        });
        tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "tv2响应", Toast.LENGTH_SHORT).show();

            }
        });
        tv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "tv3响应", Toast.LENGTH_SHORT).show();

            }
        });
        tv4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "tv4响应", Toast.LENGTH_SHORT).show();

            }
        });


    }

    private void btnSheetEvent() {
        btn_sheet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isOpen) {
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                } else {
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                }
                isOpen = !isOpen;
            }
        });
    }

    private void bottomSheetEvent() {

        bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
//                Toast.makeText(MainActivity.this,"newState = " + newState, Toast.LENGTH_SHORT).show();
                Log.e(TAG, "newState: " + newState);
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
//                Toast.makeText(MainActivity.this,"slideOffset = " + slideOffset, Toast.LENGTH_SHORT).show();
                Log.e(TAG, "slideOffset: " + slideOffset);
            }
        });
    }



    private void intiview() {
        bottomSheet = findViewById(R.id.bottom_sheet);
        bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
        btn_sheet = findViewById(R.id.btn_sheet);

        tv1 = findViewById(R.id.tv1);
        tv2 = findViewById(R.id.tv2);
        tv3 = findViewById(R.id.tv3);
        tv4 = findViewById(R.id.tv4);
        btn_Dialog = findViewById(R.id.btn_Dialog);

    }
}
